package com.example.sihookuiklydemo.adapter

import android.content.Context
import android.content.res.Configuration
import android.content.res.Resources
import android.graphics.Typeface
import android.util.DisplayMetrics
import android.util.Log
import android.view.Display
import android.view.WindowManager
import com.example.sihookuiklydemo.KRApplication
import com.tencent.kuikly.core.render.android.adapter.IKRFontAdapter
import me.jessyan.autosize.AutoSizeConfig

object KRFontAdapter : IKRFontAdapter {
    var isPad = false

    init {
        isPad = isPad(KRApplication.application)
    }

    open fun isPad(context: Context): Boolean {
        val isPad: Boolean = (context.resources.configuration.screenLayout
                and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE
        val wm: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display: Display = wm.getDefaultDisplay()
        val dm = DisplayMetrics()
        display.getMetrics(dm)
        val x = Math.pow((dm.widthPixels / dm.xdpi).toDouble(), 2.0)
        val y = Math.pow((dm.heightPixels / dm.ydpi).toDouble(), 2.0)
        val screenInches = Math.sqrt(x + y) // 屏幕尺寸
        return isPad || screenInches >= 7.0
    }

    override fun getTypeface(fontFamily: String, result: (Typeface?) -> Unit) {
        if (fontFamily.isEmpty()) {
            result(null)
        } else {
            var tfe: Typeface? = null
            when (fontFamily) {
                "Qvideo Digit" -> {
                    tfe = Typeface.createFromAsset(
                        KRApplication.application.assets,
                        "fonts/$fontFamily.ttf"
                    )
                }
            }
            result(tfe)
        }
    }


    override fun getDisplayMetrics(useHostDisplayMetrics: Boolean?): DisplayMetrics? {
        return KRApplication.application?.resources?.displayMetrics
    }
}